<!--
 * @Description: 第二十章（keep-alive缓存组件）
 * @Author: IA ia@kepro.space
 * @Date: 2025-02-17 11:55:27
-->
<template>
  <div>
    <h2>我是B组件</h2>
    <div style="margin-top: 50px">
      <el-transfer v-model="value" :data="data" />
    </div>
  </div>
</template>
<script setup lang="ts">
import { ref, reactive, onMounted, onUnmounted, onActivated, onDeactivated } from 'vue'
interface Option {
  key: number
  label: string
  disabled: boolean
}

const generateData = () => {
  const data: Option[] = []
  for (let i = 1; i <= 15; i++) {
    data.push({
      key: i,
      label: `Option ${i}`,
      disabled: i % 4 === 0,
    })
  }
  return data
}

const data = ref<Option[]>(generateData())
const value = ref([])

onMounted(() => {
  console.log('B组件 onMounted 初始化')
})

// keep-alive 新增了两个生命周期 onActivated 和 onDeactivated
// 只有开启了keep-alive才会有这两个生命周期
onActivated(() => {
  console.log('B组件 onActivated keep-alive 初始化')
})

onDeactivated(() => {
  console.log('B组件 onDeactivated keep-alive 卸载')
})

onUnmounted(() => {
  console.log('B组件 onUnmounted 卸载')
})
</script>
<style scoped></style>
